package io.vincentwinner.systeminfoapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.vincentwinner.systeminfoapi.dao.ICentralProcessUnitDao;
import io.vincentwinner.systeminfoapi.dao.IDiskDao;
import io.vincentwinner.systeminfoapi.dao.IMemoryDao;
import io.vincentwinner.systeminfoapi.dao.IOperatingSystemDao;
import io.vincentwinner.systeminfoapi.service.IDynamicInfoService;
import io.vincentwinner.toolset.os.Disk;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @see IDynamicInfoService
 */
@Service("dynamicInfoService")
public class DynamicInfoServiceImpl implements IDynamicInfoService {

    @Resource(name = "osDao")
    private IOperatingSystemDao os;

    @Resource(name = "cpuDao")
    private ICentralProcessUnitDao cpu;

    @Resource(name = "memoryDao")
    private IMemoryDao memory;

    @Resource(name = "diskDao")
    private IDiskDao disk;

    /**
     * <pre>
     *      {
     *          processesCount: 当前系统进程数量,
     *          threadCount:    当前系统线程数量,
     *          sinceBootTime:  自启动后的时间(ms)
     *      }
     * </pre>
     * @return 操作系统动态信息
     */
    @Override
    public JSONObject getOperatingSystemDynamicInfo() {
        JSONObject jsonObject = new JSONObject(3);
        jsonObject.put("processesCount",os.getProcessesCount());
        jsonObject.put("threadCount",os.getThreadCount());
        jsonObject.put("sinceBootTime",os.sinceBootTime());
        return jsonObject;
    }

    /**
     * <pre>
     *     {
     *         cpuUsage: cpu使用率
     *     }
     * </pre>
     * @return cpu动态信息
     */
    @Override
    public JSONObject getCentralProcessUnitDynamicInfo() {
        JSONObject jsonObject = new JSONObject(1);
        jsonObject.put("cpuUsage",cpu.getUsage());
        return jsonObject;
    }

    /**
     * <pre>
     *     {
     *         physicalTotal: 总物理内存空间(byte)
     *         physicalFree:  物理内存空闲空间(byte),
     *         physicalUsed:  已使用的物理内存空间(byte)
     *         physicalUsage: 物理内存使用率,
     *         swapTotal:     交换分区总空间(byte)
     *         swapFree:      交换分区空闲空间(byte),
     *         swapUsed:      已使用的交换分区空间(byte)
     *         swapUsage:     交换分区使用率
     *     }
     * </pre>
     * @return 内存动态信息
     */
    @Override
    public JSONObject getMemoryDynamicInfo() {
        JSONObject jsonObject = new JSONObject(4);
        jsonObject.put("physicalTotal",memory.getPhysicalTotal());
        jsonObject.put("physicalFree",memory.getPhysicalFree());
        jsonObject.put("physicalUsed",memory.getPhysicalUsed());
        jsonObject.put("physicalUsage",memory.getPhysicalUsage());
        jsonObject.put("swapTotal",memory.getPhysicalTotal());
        jsonObject.put("swapFree",memory.getSwapFree());
        jsonObject.put("swapUsed",memory.getSwapUsed());
        jsonObject.put("swapUsage",memory.getSwapUsage());
        return jsonObject;
    }

    /**
     * <pre>
     *     [
     *          {
     *              uuid: 唯一识别码,
     *              freeSpace: 空闲空间(byte),
     *              usedSpace: 已使用空间(byte),
     *              usage: 空间使用率
     *          },
     *          {...},
     *          ...
     *     ]
     * </pre>
     * @return 磁盘动态信息
     */
    @Override
    public JSONArray getDiskDynamicInfo() {
        JSONArray jsonArray = new JSONArray();
        List<Disk.DiskHardwareInfo> diskHardwareInfos = disk.getDiskHardwareInfos();
        for (Disk.DiskHardwareInfo diskHardwareInfo : diskHardwareInfos) {
            JSONObject jsonObject = new JSONObject(4);
            jsonObject.put("uuid",diskHardwareInfo.getUuid());
            jsonObject.put("freeSpace",diskHardwareInfo.getFreeSpace());
            jsonObject.put("usedSpace",diskHardwareInfo.getUsedSpace());
            jsonObject.put("usage",diskHardwareInfo.getUsage());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
